package com.mnyc.service;

import com.mnyc.http.FeignTestClient;
import com.mnyc.model.User;
import feign.Feign;
import feign.form.spring.SpringFormEncoder;
import feign.optionals.OptionalDecoder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.ResponseEntityDecoder;
import org.springframework.cloud.openfeign.support.SpringDecoder;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.cloud.openfeign.support.SpringMvcContract;
import org.springframework.stereotype.Service;

@Slf4j
@Service
//网上说的需要在调用的地方导入该配置类，最后我屏蔽掉该代码好像也可以
//@Import(FeignClientsConfiguration.class)
public class FeignTestService {

    @Autowired
    ObjectFactory<HttpMessageConverters> messageConverters;
    String flag = "TestClient";

    private FeignTestClient createFeignClient() {
        //1、在创建Feign客户端的时候最核心的对象是decoder、encoder、contract
        //通过跟踪源码与SpringBoot自动创建的Feign对象比较，设置decoder、encoder、
        //contract为SpringBoot中自动创建对象相同，然后定义Feign接口的时候，
        //各种参数的注解和方法的注解就可以和不动态修改url的相同了
        //decoder解码器，对返回的结果进行解码
        OptionalDecoder decoder = new OptionalDecoder(new ResponseEntityDecoder(new SpringDecoder(messageConverters)));
        //encoder编码器，对输入的数据进行编码
        SpringEncoder springEncoder = new SpringEncoder(messageConverters);
        SpringFormEncoder encoder = new SpringFormEncoder(springEncoder);
        //该对象是将接口进行解析，方便生成最后调用的网络对象HttpurlConnection
        SpringMvcContract contract = new SpringMvcContract();
        FeignTestClient feignTestClient = Feign.builder()
                .decoder(decoder)
                .encoder(encoder)
                .contract(contract)
                //这个地方的Url可以根据每次调用的时候进行改变
                .target(FeignTestClient.class, "http://localhost:8080/server");//传入url
        return feignTestClient;
    }


    public void get() {
        try {
            FeignTestClient feignClient = createFeignClient();
            String result = feignClient.getMethod(flag);
            log.info("TEST ========= " + result);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }


    public void post() {
        try {
            String result = "";
            User user = new User();
            user.setName("name");
            user.setAge(100);
            FeignTestClient feignClient = createFeignClient();
            result = feignClient.postMethod(flag, user);
            log.info("TEST ========= " + result);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void put() {
        try {
            String result = "";
            User user = new User();
            user.setName("name");
            user.setAge(100);
            FeignTestClient feignClient = createFeignClient();
            result = feignClient.putMethod(flag, user);
            log.info("TEST ========= " + result);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void delete() {
        try {
            String result = "";
            User user = new User();
            user.setName("name");
            user.setAge(100);
            FeignTestClient feignClient = createFeignClient();
            result = feignClient.deleteMethod(flag);
            log.info("TEST ========= " + result);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void path() {
        try {
            String result = "";
            User user = new User();
            user.setName("name");
            user.setAge(100);
            FeignTestClient feignClient = createFeignClient();
            result = feignClient.pathMethod(flag, user);
            log.info("TEST ========= " + result);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
